<?php
class BusinessLogTool
{
	/**
	 * 员工信息
	 * Enter description here ...
	 * @param unknown_type $array
	 */
    static function BusinessLog($type,$operator,$content,$param,$business,$receive,$receive_flg=0,$sms_flg=0,$msg_flg=1) {
    	//参数检查
    	$p['type']=$type;
    	$p['sms_flg']=$sms_flg;
    	$p['msg_flg']=$msg_flg;
    	$p['business_id']=$business;
    	$p['receive_flg']=$receive_flg;
    	if(is_numeric($operator) && $content!=''){
    		$p['operator']=$operator;
    	}else{
    		return false;
    	}
    	
    	
    	//判断是否是一组接收者
    	$receiveArray=explode(',',$receive);
        $receiveArray=array_unique($receiveArray); 
        //V($receiveArray);
        
        //循环写日志
        foreach ($receiveArray as $item){
        	if(is_numeric($item)){
        		$p['receive']=$item;
        		//整理信息
		    	$pa=$param;
		    	$pa[1]=StaffModel::getInstance()->getStaffByName($item);
		    	$p['content']=BusinessLogTool::BusinessContent($content,$pa);
    	
    			//写数据
        		$rs = BusinessLogModel::getInstance()->setBusinessLog($p);
        		if($rs==0){
        			return false;
        		}
        		
        		//写短信
        		$phone = StaffModel::getInstance()->getStaffByPhone($p['receive']);
        		message_send($phone, $p['content']);
        	}
        }
        return true;
    }
    
	/**
	 * 顾客信息
	 * Enter description here ...
	 * @param unknown_type $array
	 */
    static function BusinessSLog($type,$operator,$content,$param,$business,$receive,$receive_flg=0,$sms_flg=0,$msg_flg=1) {
    	//参数检查
    	$p['type']=$type;
    	$p['sms_flg']=$sms_flg;
    	$p['msg_flg']=$msg_flg;
    	$p['business_id']=$business;
    	$p['receive_flg']=$receive_flg;
    	if(is_numeric($operator) && $content!=''){
    		$p['operator']=$operator;
    	}else{
    		return false;
    	}
        
        //循环写日志
        $pp['id']=$p['business_id'];
        $business = BusinessModel::getInstance()->getCustomerByID($pp);
        $p['receive']=$business;
        
        //整理信息
		$p['content']=BusinessLogTool::BusinessContent($content,$param);
    	
    	//写数据
        $rs = BusinessLogModel::getInstance()->setBusinessLog($p);
        if($rs==0){
        	return false;
        }
        		
        //写短信
        $phone = CustomerModel::getInstance()->getCustomerByPhone($business);
        message_send($phone, $p['content']);

        return true;
    }
    
    /**
	 * 替换内容
	 * Enter description here ...
	 * @param unknown_type $array
	 */
    static function BusinessContent($msg,$array){
    	foreach ($array as $key=>$val){
    		$msg = str_replace("{".$key."}",$val,$msg);
    	}
    	
    	return $msg;
    }
}